package com.xiaoke.web.controller;

import com.xiaoke.web.config.shiro.IncorrectCaptchaException;
import com.xiaoke.web.service.AdminService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class HomeController extends SupportAction{
    @Autowired
    private AdminService adminService;

    @RequestMapping("/")
    public String index(Model model) {
        return "index";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest req, Model model) throws Exception {

        String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
        String username = getString(req, "username");
        String error = null;
        if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户不存在";
        } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (AuthenticationException.class.getName().equals(exceptionClassName)) {
            error = "无权限访问该目录";
        } else if (IncorrectCaptchaException.class.getName().equals(exceptionClassName)) {
            error = "验证码错误";
        }else if (exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }

        model.addAttribute("username", username);
        model.addAttribute("error", error);
        if (req.getParameter("forceLogout") != null) {
            model.addAttribute("error", "您已经被管理员强制退出，请重新登录");
        }
        return "login";
    }

}